<?php

namespace app\index\controller;

use tian\Session;

class Club extends Base
{
    public function index()
    {
        try {
            $role_id = Session::get('uid');
            $modelClub = model('Club');
            $modelPlayer = model('ClubPlayer');

            $info = $modelPlayer->getWhereInfo(['uid' => $role_id, 'status' => 2]);
            if (empty($info)) {
                $temp = 'list';

                $list = $modelClub->getList([], 'id asc');
                if (!empty($list)) {
                    foreach ($list as $k => $vo) {
                        $vo['str'] = str_base64_encode($vo['id'], $role_id);
                    }
                }
                $this->assign('list', $list);
            } else {
                $temp = 'index';

                //门派
                $infoClub = $modelClub->getInfo($info['clubid']);
                //申请
                $listApply = $this->getClubMemberList($info['clubid'], 1);
                //门派成员
                $listAdopt = $this->getClubMemberList($info['clubid'], 2);

                $this->assign('infoClub', $infoClub);
                $this->assign('listApply', $listApply);
                $this->assign('listAdopt', $listAdopt);

                $this->assign('numAdopt', count($listAdopt));
                $this->assign('maxAdopt', $infoClub['lv'] * 20);
            }

            $this->assign('role_id', $role_id);
            $this->fetch($temp);
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    private function getClubMemberList($clubid, $status)
    {
        try {
            $role_id = Session::get('uid');
            $modelPlayer = model('ClubPlayer');
            $modelRole = model('Role');

            $list = $modelPlayer->getList(['clubid' => $clubid, 'status' => $status],'id asc');
            if (!empty($list)) {
                foreach ($list as $key => $item) {
                    $infoRole = $modelRole->getInfo($item['uid']);
                    $item['uid_lv'] = $infoRole['ulv'];
                    $item['uid_state'] = state_hone($infoRole['ulv'])['name'];
                    $item['uid_name'] = $infoRole['uname'];
                    if ($infoRole['sfzx'] == 1 && rtdate(timestamp($infoRole['endtime'])) === date('Y-m-d')) {
                        $item['sfzx'] = 1;
                    } else {
                        $item['sfzx'] = 0;
                    }

                    $item['str'] = str_base64_encode($item['id'], $role_id);
                }
            }
            return $list;
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    public function apply()
    {
        try {
            $role_id = Session::get('uid');

            $str = input('str');
            $id = $this->strAnalysis($str, $role_id);
            $info = model('Club')->getInfo($id);
            if (empty($info)) {
                returnJson(0, 'error');
            }

            $infoPlayer = model('ClubPlayer')->getWhereInfo(['clubid' => $id, 'uid' => $role_id]);
            if (!empty($infoPlayer)) {
                returnJson(0, '已申请');
            }

            $add = [];
            $add['clubid'] = $id;
            $add['uid'] = $role_id;
            $add['status'] = 1;
            $add['addtime'] = time();
            model('ClubPlayer')->add($add);

            returnJson(1, 'ok');
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    public function clubAct()
    {
        try {
            $role_id = Session::get('uid');
            $modelClub = model('Club');
            $modelPlayer = model('ClubPlayer');
            $modelRole = model('Role');

            $act = input('act');
            $str = input('str');

            if ($act == 'create') {//创建
                $name = input('name');
                $info = input('info');
                if($name=='' || $info==''){
                    returnJson(0, '请输入名称和简介');
                }

                $infoClub = $modelClub->getWhereInfo(['name'=>$name]);
                if(!empty($infoClub)){
                    returnJson(0, '名称已存在');
                }

                $infoPlayer = $modelPlayer->getWhereInfo(['uid' => $role_id, 'status' => 2]);
                if(!empty($infoPlayer)){
                    returnJson(0, '请先退出门派');
                }

                $infoRole = $modelRole->getInfo($role_id);
                if($infoRole['uczb'] < 10 || $infoRole['uyxb'] < 100){
                    returnJson(0, '财物不足');
                }

                //创建
                $add['uid'] = $role_id;
                $add['name'] = $name;
                $add['info'] = $info;
                $add['lv'] = 1;
                $add['exp'] = 0;
                $add['yxb'] = 100;
                $getId = $modelClub->add($add);
                if($getId){
                    $mapAdd['clubid'] = $getId;
                    $mapAdd['uid'] = $role_id;
                    $mapAdd['status'] = 2;
                    $mapAdd['addtime'] = time();
                    $modelPlayer->add($mapAdd);

                    $tempRole = [
                        'uid'=>$role_id,
                        'uczb'=>$infoRole['uczb'] - 10,
                        'uyxb'=>$infoRole['uyxb'] - 100,
                    ];
                    $modelRole->saved($tempRole);
                }else{
                    returnJson(0, '系统繁忙');
                }
            }

            if ($act == 'dissolution') {//解散
                $infoClub = $modelClub->getWhereInfo(['uid' => $role_id]);
                if (empty($infoClub)) {
                    returnJson(0, '非法操作');
                }

                $modelClub->del($infoClub['id']);
                $modelClub->delAll(['clubid' => $infoClub['id']]);
            }

            if ($act == 'adopt') {//通过
                $id = $this->strAnalysis($str, $role_id);
                $infoPlayer = $modelPlayer->getInfo($id);
                if (empty($infoPlayer)) {
                    returnJson(0, '非法操作');
                }
                $infoClub = $modelClub->getInfo($infoPlayer['clubid']);
                if (empty($infoClub) || $infoClub['uid'] != $role_id) {
                    returnJson(0, '非法操作');
                }

                $temp = ['id' => $id, 'status' => 2];
                $modelPlayer->saved($temp);
            }

            if ($act == 'refuse') {//拒绝
                $id = $this->strAnalysis($str, $role_id);
                $infoPlayer = $modelPlayer->getInfo($id);
                if (empty($infoPlayer)) {
                    returnJson(0, '非法操作');
                }
                $infoClub = $modelClub->getInfo($infoPlayer['clubid']);
                if (empty($infoClub) || $infoClub['uid'] != $role_id) {
                    returnJson(0, '非法操作');
                }

                $modelPlayer->del($id);
            }

            if ($act == 'abdication') {//禅让
                $id = $this->strAnalysis($str, $role_id);
                $infoPlayer = $modelPlayer->getInfo($id);
                if (empty($infoPlayer)) {
                    returnJson(0, '非法操作');
                }
                $infoClub = $modelClub->getInfo($infoPlayer['clubid']);
                if (empty($infoClub) || $infoClub['uid'] != $role_id) {
                    returnJson(0, '非法操作');
                }

                $temp = [
                    'id'=>$infoPlayer['clubid'],
                    'uid'=>$infoPlayer['uid'],
                ];
                $modelClub->saved($temp);
            }

            if ($act == 'eviction') {//逐出
                $id = $this->strAnalysis($str, $role_id);
                $infoPlayer = $modelPlayer->getInfo($id);
                if (empty($infoPlayer)) {
                    returnJson(0, '非法操作');
                }
                $infoClub = $modelClub->getInfo($infoPlayer['clubid']);
                if (empty($infoClub) || $infoClub['uid'] != $role_id) {
                    returnJson(0, '非法操作');
                }

                $modelPlayer->del($id);
            }

            if ($act == 'exit') {//退出
                $id = $this->strAnalysis($str, $role_id);
                $infoPlayer = $modelPlayer->getInfo($id);
                if (empty($infoPlayer)) {
                    returnJson(0, '非法操作');
                }
                if ($infoPlayer['uid'] != $role_id) {
                    returnJson(0, '非法操作');
                }

                $modelPlayer->del($id);
            }

            returnJson(1, 'ok');
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    public function create()
    {
        try {
            $this->fetch('create');
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    public function dissolution()
    {
        try {
            $role_id = Session::get('uid');
            $modelClub = model('Club');

            $infoClub = $modelClub->getWhereInfo(['uid'=>$role_id]);
            if(empty($infoClub)){
                redirect('/index/home/index');
            }
            $this->assign('infoClub', $infoClub);

            $this->fetch('dissolution');
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

}
